package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Chance;
import com.sl.au.entity.ChanceDelivery;
import com.sl.au.entity.ChanceDeliveryTemp;
import com.sl.au.entity.User;
import com.sl.au.repository.ChanceDeliveryRepository;
import com.sl.au.repository.ChanceDeliveryTempRepository;
import com.sl.au.service.ChanceDeliveryService;
import com.sl.au.service.ChanceFlowNodeService;
import com.sl.au.service.ChanceService;
import com.sl.au.service.OperateLogService;

@Service("ChanceDeliveryService")
@Transactional
public class ChanceDeliveryServiceImpl extends BaseServiceImpl implements ChanceDeliveryService{

	@Autowired
	ChanceService chanceService;
	@Autowired
	ChanceDeliveryRepository chanceDeliveryRepository;
	
	@Autowired
	ChanceDeliveryTempRepository chanceDeliveryTempRepository;
	@Autowired
	ChanceFlowNodeService chanceFlowNodeService;
	
	@Autowired
	OperateLogService operateLogService;
	
	@Override
	public ChanceDeliveryRepository getRepository() {
		// TODO Auto-generated method stub
		return this.chanceDeliveryRepository;
	}

	@Override
	public List<Chance> getPage(int start, int length, Map<String,String> condition,int targetType, String orderName, String dir) {
		Page<Chance> pages=null;
		String dataFilter=condition.get("dataFilter");
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		if(dataFilter.equals("本科室"))
			//pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"), pageable);
			if(targetType == 4){
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"), pageable);
			}else{
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"),targetType, pageable);
			}
		else
			if(targetType == 4){
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"), pageable);
			}else{
				pages = chanceDeliveryRepository.findUserChanceOne(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),targetType, pageable);
			}
			//pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"), pageable);
		return pages.getContent();
	}
	
	@Override
	public List<Chance> getPage(int start, int length, Map<String,String> condition, String orderName, String dir) {
		Page<Chance> pages=null;
		String dataFilter=condition.get("dataFilter");
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		if(dataFilter.equals("本科室"))
			pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"), pageable);
			/*if(targetType == 4){
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"), pageable);
			}else{
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),condition.get("sectionId"),targetType, pageable);
			}*/
		else
			/*if(targetType == 4){
				pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"), pageable);
			}else{
				pages = chanceDeliveryRepository.findUserChanceOne(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"),targetType, pageable);
			}*/
			pages = chanceDeliveryRepository.findUserChance(condition.get("userSuperior"),condition.get("key"),condition.get("isReceived"), pageable);
		return pages.getContent();
	}

	@Override
	public List<ChanceDelivery> getModulePage(int start, int length, Map<String,Object> condition, String orderName, String dir) {
		Page<ChanceDelivery> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		pages = chanceDeliveryRepository.findModuleArea((String)condition.get("chanceId"),(Integer)condition.get("targetType"),
				(String)condition.get("userSuperior"),pageable);
		return pages.getContent();
	}
	@Override
	public List<ChanceDeliveryTemp> getModulePageCoop(int start, int length, String chanceId, String orderName, String dir) {
		Page<ChanceDeliveryTemp> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		pages = chanceDeliveryRepository.findModuleAreaCoop(chanceId, pageable);
		return pages.getContent();
	}

	@Override
	public List<ChanceDelivery> getManagerPage(int start, int length, Map<String, String> condition,int targetType, String orderName,
			String dir) {
		Page<ChanceDelivery> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		if(targetType == 4){
			pages = chanceDeliveryRepository.findManagerChance(condition.get("userSuperior"),condition.get("userArea"),
					condition.get("key"),condition.get("isReceived"), pageable);
		}else{
			pages = chanceDeliveryRepository.findManagerChance(condition.get("userSuperior"),condition.get("userArea"),
					condition.get("key"),condition.get("isReceived"),targetType, pageable);
		}
		return pages.getContent();
	}
	
	@Override
	public List<ChanceDelivery> getManagerPage(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		Page<ChanceDelivery> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
			pages = chanceDeliveryRepository.findManagerChance(condition.get("userSuperior"),condition.get("userArea"),
					condition.get("key"),condition.get("isReceived"), pageable);
		
		return pages.getContent();
	}

	@Override
	public ChanceDeliveryTempRepository getDeliveryTempRepository() {
		// TODO Auto-generated method stub
		return this.chanceDeliveryTempRepository;
	}

	@Override
	public void closeChance(User user,String chanceId,String reason,String contractCode,String result) {
		Chance c=this.chanceService.getRepository().getOne(chanceId);
		if(null==c) return;
		c.setCloseReason(reason);
		c.setContractCode(contractCode);
		c.setIsSucceed(result);
		this.chanceService.getRepository().save(c);
		
		chanceFlowNodeService.flow(user, c, "","关闭","关闭信息");
		
		String operateType = "信息分配片区";
		String content = "关闭销售信息【"+c.getName()+"】";
		
		operateLogService.log(user, operateType, content);
		
		
		
	}
}
